package com.ctg.behavior.calc.impala.transfer.usergroups;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.ctg.behavior.calc.common.CtSQLParam;
import com.ctg.behavior.calc.impala.CtSQLTransfer;
import com.ctg.behavior.calc.impala.builder.usergroups.CtUGCalcReportBuilder;
import com.ctg.behavior.calc.impala.report.uesrgroups.CtCalcUGUBRuleReport;
import com.ctg.behavior.calc.impala.report.uesrgroups.CtUGUBBehaviorReport;
import com.ctg.behavior.common.calc.CtCalc;
import com.ctg.behavior.common.calc.CtCalcUserBehavior;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class CtUGUBRuleTransfer implements CtSQLTransfer {

	@Autowired
	private CtUGUBBehaviorTransfer ubBehaviorTransfer;
	@Autowired
	private CtUGCalcReportBuilder ugCalcReportBuilder;

	/******************************************************
	 * @apiNote: 根据用户行为Rule来转化成ImpalaSQL
	 * @apiNote: Rule下面有多个Behavior
	 */
	@Override
	public String doTransfer(CtCalc calcObject, CtSQLParam sqlParam) {
		if (!(calcObject instanceof CtCalcUGUBRuleReport)) {
			log.warn("calcObject is not CtCalcUGUBRuleReport, class={}", calcObject.getClass());
			log.warn("calcObject: {}", JSON.toJSON(calcObject));
			return "";
		}
		CtCalcUGUBRuleReport ubRule = (CtCalcUGUBRuleReport) calcObject;
		log.info("CalcBody={}", JSON.toJSONString(ubRule));

		// 此层SQL的表查询别名
		String thisAlias = "ugub";

		StringBuilder impalaSQL = new StringBuilder();

		impalaSQL.append(" SELECT DISTINCT ").append(thisAlias).append(".user_id AS user_id ");
		impalaSQL.append(" FROM a_event_parquet_").append(ubRule.getAppId());
		// 增加别名
		impalaSQL.append(" ").append(thisAlias).append(" ");
		impalaSQL.append(" WHERE 1=1 ");

		String behaviorsRelation = ubRule.getRelation();
		List<CtCalcUserBehavior> behaviors = ubRule.getBehaviors();

		if (behaviors.isEmpty()) {
			// 若用户行为规则没有指定，则不增加此SQL条件
			return "";
		}

		CtSQLParam ubBrehaviorSQLParam = CtSQLParam.createAliasParam(thisAlias);

		impalaSQL.append(" AND ( ");
		for (int behaviorIndex = 0; behaviorIndex < behaviors.size(); behaviorIndex++) {

			// 把用户行为Rule中的一个CtCalcUserBehavior转换为计算SQL中的Where条件
			CtCalcUserBehavior oneUserBehavior = behaviors.get(behaviorIndex);
			CtUGUBBehaviorReport ubBehaviorReport = null;
			ubBehaviorReport = ugCalcReportBuilder.buildUGUBBehaviorReport(oneUserBehavior, ubRule.getAppId());

			String ubBehaviorSQL = ubBehaviorTransfer.doTransfer(ubBehaviorReport, ubBrehaviorSQLParam);
			impalaSQL.append(ubBehaviorSQL);
			// 当有多个CtCalcUserBehavior时，根据用户行为的Relation字段来定：OR/AND
			if (behaviorIndex < behaviors.size() - 1) {
				impalaSQL.append(behaviorsRelation.toUpperCase()).append(" ");
			}
		}
		impalaSQL.append(" ) ");

//		// GROUP BY devcie_id
//		impalaSQL.append(" GROUP BY ").append(thisAlias).append(".device_id ");

		String retSQL = impalaSQL.toString();
		log.info("CalcSQL={}", retSQL);

		return retSQL;
	}

}
